﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class EquipmentRecord
    {
        //报废记录
        public class Abandon
        {
            //获取报废记录列表
            public static List<Model.EquipmentAbandon> GetList(string strWhere = "")
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("SELECT * FROM EquipmentAbandon");
                if (strWhere.Trim() != "")
                {
                    sql.Append(" WHERE " + strWhere);
                }
                SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.sqlConnectionString, CommandType.Text, sql.ToString());
                List<Model.EquipmentAbandon> list = new List<Model.EquipmentAbandon>();
                if (dr.HasRows)
                {
                    Model.EquipmentAbandon m;
                    while (dr.Read())
                    {
                        m = new Model.EquipmentAbandon();
                        m.EID = int.Parse(dr[0].ToString());
                        m.checker = dr[1].ToString();
                        m.date = Convert.ToDateTime(dr[2].ToString());
                        m.cause = dr[3].ToString();
                        m.Equipment = DAL.LabEquipmentService.SelectLabEquipmentByID(m.EID);
                        m.Equipment.typeParam = DAL.ParameterService.SelectOne(m.Equipment.type);
                        m.Equipment.usetypeParam = DAL.ParameterService.SelectOne(m.Equipment.usetype);
                        list.Add(m);
                    }
                    dr.Close();
                }
                dr.Close();
                return list;

            }
        }

        //租借记录
        public class Rent
        {
            //获取租借记录列表
            public static List<Model.EquipmentRent> GetList(string strWhere = "")
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("SELECT * FROM EquipmentRent");
                if (strWhere.Trim() != "")
                {
                    sql.Append(" WHERE " + strWhere);
                }
                SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.sqlConnectionString, CommandType.Text, sql.ToString());
                List<Model.EquipmentRent> list = new List<Model.EquipmentRent>();
                if (dr.HasRows)
                {
                    Model.EquipmentRent m;
                    while (dr.Read())
                    {
                        m = new Model.EquipmentRent();
                        m.EID = int.Parse(dr[1].ToString());
                        m.renter = dr[2].ToString();
                        m.renterTel = dr[3].ToString();
                        m.used = dr[4].ToString();
                        m.rentDate = Convert.ToDateTime(dr[5].ToString());
                        m.timeDay = int.Parse(dr[6].ToString());
                        m.allower = dr[7].ToString();
                        if (dr[8].ToString() != "")
                        {
                            m.returnDate = Convert.ToDateTime(dr[8].ToString());
                        }
                        else
                        {
                            m.returnDate = null;
                        }
                        m.checker = dr[9].ToString();
                        m.isReturn = Convert.ToBoolean(dr[10].ToString());
                        m.Equipment = DAL.LabEquipmentService.SelectLabEquipmentByID(m.EID);
                        m.Equipment.typeParam = DAL.ParameterService.SelectOne(m.Equipment.type);
                        list.Add(m);
                    }
                    dr.Close();
                }
                dr.Close();
                return list;
            }
            //根据EID查询一条借出未归还记录
            public static Model.EquipmentRent SelectRentNoReturnByEID(int EID)
            {
                string sql = string.Format("SELECT * FROM EquipmentRent WHERE EID = {0} AND isReturn = 0", EID);
                SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.sqlConnectionString, CommandType.Text, sql);
                Model.EquipmentRent m = new Model.EquipmentRent();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        m.id = int.Parse(dr[0].ToString());
                        m.EID = int.Parse(dr[1].ToString());
                        m.renter = dr[2].ToString();
                        m.allower = dr[7].ToString();
                        m.rentDate = Convert.ToDateTime(dr[5]);
                        m.timeDay = int.Parse(dr[6].ToString());
                        m.Equipment = DAL.LabEquipmentService.SelectLabEquipmentByID(m.EID);
                    }
                    dr.Close();
                }
                dr.Close();
                return m;
            }
        }

        //报修记录
        public class Repair
        {
            //获取维修记录列表
            public static List<Model.EquipmentRepair> GetList(string strWhere = "")
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("SELECT * FROM EquipmentRepair");
                if (strWhere.Trim() != "")
                {
                    sql.Append(" WHERE " + strWhere);
                }
                SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.sqlConnectionString, CommandType.Text, sql.ToString());
                List<Model.EquipmentRepair> list = new List<Model.EquipmentRepair>();
                if (dr.HasRows)
                {
                    Model.EquipmentRepair m;
                    while (dr.Read())
                    {
                        m = new Model.EquipmentRepair();
                        m.ID = int.Parse(dr[0].ToString());
                        m.EID = int.Parse(dr[1].ToString());
                        m.reportDate = Convert.ToDateTime(dr[2].ToString());
                        m.allower = dr[3].ToString();
                        m.RepairType = int.Parse(dr[4].ToString());
                        m.RepairCause = dr[5].ToString();
                        m.budget = decimal.Parse(dr[6].ToString());
                        m.repairer = dr[7].ToString();
                        if (dr[8].ToString() != "")
                        {
                        m.timeDay = int.Parse(dr[8].ToString());
                        }
                        if (dr[9].ToString() != "")
                        {
                            m.cost = decimal.Parse(dr[9].ToString());
                        }
                        m.result = dr[10].ToString();
                        m.checker = dr[11].ToString();
                        if (dr[12].ToString() != "")
                        {
                            m.returnDate = Convert.ToDateTime(dr[12]);
                        }
                        else
                        {
                            m.returnDate = null;
                        }
                        m.isReturn = Convert.ToBoolean(dr[13]);
                        m.Equipment = DAL.LabEquipmentService.SelectLabEquipmentByID(m.EID);
                        m.Equipment.typeParam = DAL.ParameterService.SelectOne(m.Equipment.type);
                        m.RepairTypeParam = DAL.ParameterService.SelectOne(m.RepairType);
                        list.Add(m);
                    }
                    dr.Close();
                }
                dr.Close();
                return list;
            }

            //根据EID查询一条报修未反馈记录
            public static Model.EquipmentRepair SelectRepariNoReturnByEID(int EID)
            {
                string sql = string.Format("SELECT * FROM EquipmentRepair WHERE EID = {0} AND isReturn = 0", EID);
                SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.sqlConnectionString, CommandType.Text, sql);
                Model.EquipmentRepair m = new Model.EquipmentRepair();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        m.ID = int.Parse(dr[0].ToString());
                        m.EID = int.Parse(dr[1].ToString());
                        m.reportDate = Convert.ToDateTime(dr[2]);
                        m.allower = dr[3].ToString();
                        m.RepairType = int.Parse(dr[4].ToString());
                        m.RepairTypeParam = DAL.ParameterService.SelectOne(m.RepairType);
                        m.RepairCause = dr[5].ToString();
                        m.budget = int.Parse(dr[6].ToString());
                        if (dr[8].ToString() != "")
                        {
                            m.timeDay = int.Parse(dr[8].ToString());
                        }
                        m.Equipment = DAL.LabEquipmentService.SelectLabEquipmentByID(m.EID);
                    }
                    dr.Close();
                }
                dr.Close();
                return m;
            }
        }
    }
}
